<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\UserDeviceInfo
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property int $loan_request_id 授信id
 * @property string $ip 请求ip
 * @property string $device_type 设备类型
 * @property string $device 设备型号
 * @property string $device_os 设备系统
 * @property string $device_mac 设备mac号
 * @property string|null $device_imei 设备IMEI
 * @property string|null $device_uuid 设备UUID
 * @property string $device_app APP版本号
 * @property string $gps 请求GPS （经纬度）
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDevice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDeviceApp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDeviceImei($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDeviceMac($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDeviceOs($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDeviceType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereDeviceUuid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereGps($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereLoanRequestId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereUserId($value)
 * @mixin \Eloquent
 * @property string $risk_device_id 设备号
 * @property string $risk_device_type 设备类型  1=xincheng_web 2=xincheng_and 3=xincheng_ios
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo byLoanId($loanId)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereRiskDeviceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserDeviceInfo whereRiskDeviceType($value)
 */
class UserDeviceInfo extends Model
{
    protected $fillable = [
        'user_id',
        'loan_request_id',
        'ip',
        'device_type',
        'device',
        'device_os',
        'device_mac',
        'device_imei',
        'device_uuid',
        'device_app',
        'gps',
    ];

    protected $appends = [
        'risk_device_id',
        'risk_device_type'
    ];

    /**
     * Description:定义IP访问器
     * Author: Sam
     * DateTime: 2019/1/9 21:37
     * @param $value
     * @return string
     */
    public function getIpAttribute($value)
    {
        return long2ip($value);
    }

    /**
     * Description:定义IP修改器
     * Author: Sam
     * DateTime: 2019/1/9 21:39
     * @param $value
     */
    public function setIpAttribute($value)
    {
        $this->attributes['ip'] = ip2long($value);
    }

    public function scopeByLoanId($q, $loanId)
    {
        return $q->where('loan_request_id', $loanId);
    }

    public function getRiskDeviceIdAttribute(){
        $deviceId = '';
        if($this->attributes['device_type']){
            $deviceType = strtolower($this->attributes['device_type']);
            switch ($deviceType){
                case 'android':
                    $deviceId = $this->attributes['device_imei'];
                    break;
                case 'ios':
                    $deviceId = $this->attributes['device_uuid'];
                    break;
            }
        }
        return $deviceId;
    }

    public function getRiskDeviceTypeAttribute(){
        $deviceType = strtolower($this->attributes['device_type']);
        switch ($deviceType){
            case 'android':
                $type = 2;
                break;
            case 'ios':
                $type = 3;
                break;
            case 'web':
                $type = 1;
                break;
        }
        return $type;
    }
}
